define(function (require, exports, module) {
    exports.init = function (ctx, uuid) {
        require("apps/grid");
        var page = new $.PageDomFactory(uuid);
        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "nsdb",
                colbar: "role_template"
            },
            configs: {
                gridTitle: "角色列表",
                gridUrl: ctx + "/app/api/role/queryPageForJson",
                gridColumns: [
                    {field: "id", checkbox: true},
                    {field: "roleName", title: "角色名称", width: 100},
                    {field: "roleDesc", title: "角色说明", width: 400}
                ]
            },
            events: {
                renderColBarBefore: function (type, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "edit":
                            whenEdit(context, btn.href, row);
                            break;
                        case "view":
                            whenView(context, row);
                            break;
                        case "sync":
                            whenSync(context, btn.href, row);
                            break;
                        case "resource":
                            whenSetResource(context, btn.href, row);
                            break;
                        case "user":
                            whenSetUser(context, btn.href, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "角色搜索", "role_search");
                            break;
                        case "add":
                            whenAdd(context, btn.href);
                            break;
                        case "del":
                            context.del(btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var globalWindow = new $.PageWindow();

        function whenEdit(context, href, row) {
            globalWindow.create({
                href: href + "?id=" + row.id,
                method: "role_edit_save",
                title: "编辑角色",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenView(context, row) {
            context.detail("角色详情查看", row);
        }

        function whenAdd(context, href) {
            globalWindow.create({
                href: href,
                method: "role_add_save",
                title: "新增角色",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenSetResource(context, href, row) {
            globalWindow.create({
                href: href + "?id=" + row.id,
                method: "role_resource_save",
                title: "编辑资源",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenSetUser(context, href, row) {
            globalWindow.create({
                href: href + "?id=" + row.id,
                method: "role_user_save",
                title: "编辑用户",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }
    };

    exports.add = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);

        window.role_add_save = function (win) {
            if (!page.getJqDom('saveRole').form('validate')) {
                return false;
            }
            win.wait("start", "保存中,请稍候.");
            var form = page.getJqDom("saveRole");
            var postData = form.serializeJson();
            $.post(form.attr(
                    "action")
                    + "?random=" + Math.random(), $.utils.buildJson(postData),
                function (json) {
                    win.wait("end");
                    if ($.isPlainObject(json) && json.error) {
                        $.messager.alert('信息',
                            json.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.callback(true);
                        win.close();
                    }
                }, "json");
        }
    };

    exports.update = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);

        window.role_edit_save = function (win) {
            if (!page.getJqDom('saveRole').form('validate')) {
                return false;
            }
            win.wait("start", "保存中,请稍候.");
            var form = page.getJqDom("saveRole");
            var postData = form.serializeJson();
            $.post(form.attr(
                    "action")
                    + "?random=" + Math.random(), $.utils.buildJson(postData),
                function (json) {
                    win.wait("end");
                    if ($.isPlainObject(json) && json.error) {
                        $.messager.alert('信息',
                            json.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.callback(true);
                        win.close();
                    }
                }, "json");
        }
    };

    exports.search = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);
        window.role_search = function (win) {
            win.wait("start", "搜索中,请稍候.");
            var form = page.getJqDom("searchRole");
            var postData = form.serializeJson();
            win.callback($.utils.buildJson(postData));
            win.close();
        }
    };

    require("apps/boxselect");
    exports.users = function (ctx, uuid, config) {
        var page = new $.PageDomFactory(uuid);

        page.getJqDom("userMemberIds").mutiSelect({
            url: ctx + "/app/api/sysMember/roleUsers?id=" + config.roleId,
            idFiled: "id",
            textFiled: "name"
        });

        window.role_user_save = function(win) {
            win.wait("start", "保存中,请稍候.");
            var form = page.getJqDom("dialogFromOfUseTree");
            var postData = form.serializeJson();
            $.post(ctx + "/app/api/role/saveRoleUsers?random=" + Math.random(), $.utils.buildJson(postData),
                function (json) {
                    win.wait("end");
                    if ($.isPlainObject(json) && json.error) {
                        $.messager.alert('信息',
                            json.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.close();
                    }
                }, "json");
        }
    };

    require("apps/ztree");
    exports.resource = function (ctx, uuid, config) {
        var page = new $.PageDomFactory(uuid);
        var treeId = $.utils.createUUID(6);
        page.getJqDom("resTree").attr("id", treeId);

        var setting = {
            check: {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "s", "N": "s" }
            }
        };

        function getCheckboxCheck() {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            var nodes = treeObj.getCheckedNodes(true);
            if (nodes.length == 0) {
                return "";
            }
            return nodes.map(function (item) {
                return item.id;
            }).join(",");
        }

        $.ajax({
            type: 'POST',
            dataType: "json",
            url: ctx + "/app/api/resource/roleResources",
            data: {id: config.roleId},
            success: function (data) {
                $.fn.zTree.init(page.getJqDom("resTree"), setting, data);
            }
        });

        window.role_resource_save = function(win) {
            win.wait("start", "保存中,请稍候.");
            var form = page.getJqDom("dialogFromOfResTree");
            var postData = form.serializeJson();
            postData.resources = getCheckboxCheck();
            $.post(form.attr(
                    "action")
                    + "?random=" + Math.random(), $.utils.buildJson(postData),
                function (json) {
                    win.wait("end");
                    if ($.isPlainObject(json) && json.error) {
                        $.messager.alert('信息',
                            json.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        win.close();
                    }
                }, "json");
        }
    };
});

